package com.molan.molan_music.controller;


import com.molan.molan_music.dto.MusicCreateRequest;
import com.molan.molan_music.dto.MusicUpdateRequest;
import com.molan.molan_music.mapper.MusicMapper;
import com.molan.molan_music.service.MusicService;
import com.molan.molan_music.vo.MusicVo;
import com.molan.molan_music.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Api(tags = "音乐管理接口")
@RestController
@RequestMapping("/musics")
public class MusicController {

    @Resource
    private MusicService musicService;

    @Resource
    private MusicMapper musicMapper;

    @ApiOperation(value = "创建音乐",notes = "创建音乐")
    @PostMapping("/create")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public MusicVo create(@Validated @RequestBody MusicCreateRequest musicCreateRequest){

        return musicMapper.toVo(musicService.create(musicCreateRequest));
    }

    @ApiOperation(value = "更新音乐",notes = "更新音乐")
    @PostMapping("/{id}/update")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public MusicVo update(@PathVariable Integer id, @Validated @RequestBody MusicUpdateRequest musicUpdateRequest){

        return musicMapper.toVo(musicService.update(id,musicUpdateRequest));
    }

    @ApiOperation(value = "查询音乐",notes = "查询音乐")
    @GetMapping("/list")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public List<MusicVo> list(@RequestParam int pageNum, @RequestParam int pageSize){
        return musicService.list(pageNum,pageSize).stream().map(musicMapper::toVo).collect(Collectors.toList());
    }
}
